package com.xrtn.job;

import com.xrtn.bean.InitGradeDoctor;
import com.xrtn.bean.SysUserInfo;
import com.xrtn.bean.TDoctorMonthRel;
import com.xrtn.mapper.InitGradeDoctorMapper;
import com.xrtn.mapper.SysUserInfoMapper;
import com.xrtn.mapper.TDoctorMonthRelMapper;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.*;

/**
 * Created by tu_pei on 2018/3/20.
 * 核算连续三个月连续符合升级标准
 */
public class DoctorUpwardJob {

    @Autowired
    private SysUserInfoMapper sysUserInfoMapper;

    @Autowired
    private TDoctorMonthRelMapper doctorMonthRelMapper;

    public void work(){

        Map<String, Object> params = new HashMap<>();
        params.put("userStatus", 0);// 0 在职 ， 1 离职 2辞退
        List<SysUserInfo> userList = sysUserInfoMapper.selectUserListByMap(params);

        for (SysUserInfo userInfo : userList) {

            Integer userLevel = userInfo.getUserLevel();
            //当用户等级为1级，
            if (userLevel == 1) {
                params = new HashMap<>();
                params.put("year", new Date());
                params.put("doctorId", userInfo.getUserId());
                List<TDoctorMonthRel> doctorMonthList = doctorMonthRelMapper.selectUserMonthSale(params);
                Integer count = 0;
                for (TDoctorMonthRel monthRel : doctorMonthList) {
                    //任意三个月达到向上一级标准晋升一级
                    if (monthRel.getUserLevel() == (userInfo.getUserLevel()+1) ) {
                        count++;
                    }
                    if (count == 3) {
                        //用户升级成功
                        if( userInfo.getUserLevel()  < 5 ){
                            userInfo.setUserLevel(userInfo.getUserLevel() + 1);
                            sysUserInfoMapper.updateByPrimaryKeySelective(userInfo);
                        }
                    }
                }
            } else {
                //连续3个月内
                params = new HashMap<>();
                params.put("month", 3);
                params.put("doctorId", userInfo.getUserId());
                List<TDoctorMonthRel> doctorMonthList = doctorMonthRelMapper.selectUserMonthSale(params);
                if (doctorMonthList.size() == 3) {
                    Integer count = 0;
                    for (TDoctorMonthRel monthRel : doctorMonthList) {
                        if (monthRel.getUserLevel() == (userInfo.getUserLevel()+1) ) {
                            count++;
                        }
                    }
                    //连续三个月  订单|满意度|月治愈率都达标修改用户等级
                    if( count == 3){
                        //用户升级成功
                        if( userInfo.getUserLevel() < 5 ){
                            userInfo.setUserLevel(userInfo.getUserLevel() + 1);
                            sysUserInfoMapper.updateByPrimaryKeySelective(userInfo);
                        }
                    }
                }
            }
        }
    }
}
